#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowIcon(QIcon(":/imgs/imgs/icon.png"));
    this->setWindowTitle(QString("UDP图像助手-激光组专用"));
    sysTimer = new QTimer(this);
    showImageObj = new ShowImage(ui, sysTimer);
    UdpReceiverObj = new UdpReceiver(ui);
    // 设置默认的端口号
    ui->lineEditUdpPort->setText("8887");
    // 创建一个正则表达式，匹配整数
    QRegularExpression regExp(R"(^\d*$)");
    // 创建一个QRegularExpressionValidator对象
    QRegularExpressionValidator *validator = new QRegularExpressionValidator(regExp, this);
    // 为QLineEdit设置验证器
    ui->lineEditUdpPort->setValidator(validator);
    // 设置showpushbutton为不可用状态
    ui->pushButtonShow->setDisabled(true);
    // 设置日志输出窗口可读
    ui->textEditDebug->setReadOnly(true);
    // 设置radiobutton的默认值
    ui->radioButtonOS2_128->setChecked(true);

//    this->setWindowIcon(QIcon(":/img/imgs/air.png"));

    connect(ui->pushButtonConnect,  &QPushButton::clicked,[this](){
        if(ui->pushButtonConnect->text() == "打开连接" ){
            if(!ui->lineEditUdpPort->text().isEmpty()){
                UdpReceiverObj->startReceiver(ui->lineEditUdpPort->text().toInt());
                ui->pushButtonConnect->setText("关闭连接");
                ui->pushButtonShow->setEnabled(true);
                ui->radioButtonOS1_32->setEnabled(false);
                ui->radioButtonOS2_128->setEnabled(false);
                qDebug()<<"connect success!";
            }else{
                qDebug()<<"请输入端口号";
                ui->textEditDebug->append( "请输入端口号");
            }

        }
        else{
             UdpReceiverObj->stopReceiver();
             ui->pushButtonConnect->setText("打开连接");
             ui->radioButtonOS1_32->setEnabled(true);
             ui->radioButtonOS2_128->setEnabled(true);
             ui->pushButtonShow->setDisabled(true);
        }

    });


    // 将定时器的timeout信号与槽函数连接
    connect(this->sysTimer, &QTimer::timeout, [this]{
        this->showImageObj->Show();
    });

    connect(this->ui->pushButtonSave,  &QPushButton::clicked,[this](){
        this->showImageObj->Save();
    });

    connect(this->ui->pushButtonShow,  &QPushButton::clicked,[this](){
        this->showImageObj->StartStopTimer();
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

